<?php

use yii\helpers\Html;
use yii\widgets\LinkPager;
use yii\grid\GridView;
use app\common\Functions;

/**
 * @var yii\web\View $this
 * @var amnah\yii2\user\Module $module
 * @var array $actions
 */
global $regionTypes2;
global $prodAttrCodes;
$regionTypes2 = $regionTypes;
$module = $this->context->module;

$this->title = Yii::t('user', 'Yii 2 User');



?>



    <p>
        <?php
            $createUrl                     = [];
            $createUrl[]                   = 'create';
            $createUrl['prod_spec_id']   = $prod_spec_id;
            $createUrl['prod_spec_name'] = $prodSpecName;
            $createUrl['last_url']         = $now_url;

            if ($parent_prod_id)
            {
                $createUrl['parent_prod_id'] = $parent_prod_id;
            }

            $createUrlModel                     = [];
            $createUrlModel['id']            =  'activity-create-prod-link';
            $createUrlModel['class']        = 'btn btn-sm  btn-success activity-create-prod-link';
            $createUrlModel['data-toggle'] = 'modal';
            $createUrlModel['data-target'] = '#create-prod-modal';

            $createUrlModel['label'] = '创建新产品';
            $createUrlModel['title'] = '创建新产品';


            $createUrlModel ['data-prod_spec_id']   = $prod_spec_id;
            $createUrlModel ['data-prod_spec_name'] = $prodSpecName;
            $createUrlModel ['data-last_url']         = $now_url;

            if ($parent_prod_id)
            {
                $createUrlModel['data-parent_prod_id'] = $parent_prod_id;
            }

            //创建区域逻辑
            // 如果创建大楼

        if($yw2==2 ){
            echo Html::a( Html::tag('span', '业务2部创建房源'  , ['class' => "glyphicon fa fa-plus-square"])   . $region_name. Yii::t('region', $region_type_name) , '#', $createUrlModel);

        }else{
            echo Html::a( Html::tag('span', Yii::t('region', 'Create') .  $prodSpecName , ['class' => "glyphicon fa fa-plus-square"])   . $region_name. Yii::t('region', $region_type_name) , '#', $createUrlModel);

        }

      //  echo Html::a( Html::tag('span', Yii::t('region', 'Create') .  $prodSpecName , ['class' => "glyphicon fa fa-plus-square"])   . $region_name. Yii::t('region', $region_type_name) , $createUrl, ['class' => 'btn btn-success']);

        ?>

    </p>

    <?php
        $prodAttrCodes=[];

       // Yii::trace("=====================              ============");
        //Yii::trace($dataProvider);


        // 增加展示的字段
        $columns = [];
        //$i = 0;

        // 复选框
        $i = 'check_box';
        $columns[$i]['class'] = 'yii\grid\CheckboxColumn';
        $columns[$i]['multiple'] = true;
        $columns[$i]['name'] = 'uid';
        $columns[$i]['options'] = ['style'=>'width:30px'];


        //$i++;

        // id
        $i = 'id';
        $columns[$i]['attribute'] = 'id';
        $columns[$i]['options'] = ['style'=>'width:30px'];

        //$i++;
        //名称
        $i = 'prod_name';
        $columns[$i]['label'] = '名称';
//        $columns[$i]['attribute'] = 'prod_name';
        $columns[$i]['format'] = 'raw';
        $columns[$i]['options'] = ['style'=>'width:180px'];
        $columns[$i]['value']   = function($model) use ($sub_prod_spec_id,$url ,$yw2)
        {

            $go_url[] = 'classify';
            $go_url['prod_spec_id'] = $sub_prod_spec_id;
            $go_url['parent_id']     = $model->id;
            $go_url['a']              = $url;

            if($yw2==3){
                return $model->prod_name;
            }else{
                return Html::a( $model->prod_name ,$go_url, ['title' => "收录房源"]);
            }


        };
//        $columns[$i]['filter'] = Html::activeTextInput($searchModel, 'prod_name', ['class'=>'form-control']);

        $i = 'prod_sum_areas';
        $columns[$i]['label'] = '总面积';
        $columns[$i]['format'] = 'raw';

        $columns[$i]['options'] = ['style'=>'width:90px'];
        $columns[$i]['value']   = function($model) use ($prod_spec_id)
        {
            $unit = '㎡';
            $text = 0;
            if ($prod_spec_id==10002)
            {
                $model->totalArea = $model->buildAreasFirst->sumContent;

            }
            else{
                $model->totalArea = $model->buildAreas->sumContent;
            }

            if ($model->totalArea)
            {
                $text = $model->totalArea;

            }

            return $text . $unit;
            //return Html::a(count($model->subProdInsts), ['classify', 'prod_spec_id'=>10002,   'parent_id'=> $model->id ], ['title' => "收录房源"]);
            //return $model->buildAreas->sumContent;
        };


    //$i++;
    $i = 'prod_lended_area';
    $columns[$i]['label'] = '已租面积';
    $columns[$i]['format'] = 'raw';

    $columns[$i]['options'] = ['style'=>'width:90px'];
    $columns[$i]['value']   = function($model)
    {
        $unit = '㎡';
        $model->lendedArea = 0;
        $tmp = $model->selectLendedAreas($model->id, $model->prod_spec_id);
        if ($tmp['sumContent'])
        {
            $model->lendedArea = $tmp['sumContent'];
        }
        //Yii::trace("=====================              ============");
        //  $sumLendedContent = $model->lendedProds->lendedAreas->sumLendedContent;
        //Yii::trace($tmp );
        //return Html::a(count($model->subProdInsts), ['classify', 'prod_spec_id'=>10002,   'parent_id'=> $model->id ], ['title' => "收录房源"]);
        return  $model->lendedArea . $unit;
    };

    //$i++;
    $i = 'prod_vacancy_area';
    $columns[$i]['label'] = '空置面积';
    $columns[$i]['format'] = 'raw';

    $columns[$i]['options'] = ['style'=>'width:90px'];
    $columns[$i]['value']   = function($model)
    {
        $unit = '㎡';
        if(!$model->totalArea)
        {
            $model->totalArea = 0;
        }

        if(!$model->lendedArea)
        {
            $model->lendedArea = 0;
        }

        $model->vacancyArea = $model->totalArea - $model->lendedArea;
        //return Html::a(count($model->subProdInsts), ['classify', 'prod_spec_id'=>10002,   'parent_id'=> $model->id ], ['title' => "收录房源"]);
        return $model->vacancyArea . $unit;
    };

    //$i++;
    $i = 'prod_canvass_areas';
    $columns[$i]['label'] = '可招商面积';
    $columns[$i]['format'] = 'raw';

    $columns[$i]['options'] = ['style'=>'width:90px'];
    $columns[$i]['value']   = function($model)
    {
        $unit = '㎡';
        $canvassAreas = $model->selectCanvassAreas($model->id, $model->prod_spec_id);
        $sumCanvass = 0;


        if ($canvassAreas['sumCanvass'])
        {
            $sumCanvass = $canvassAreas['sumCanvass'];
        }
        //return Html::a(count($model->subProdInsts), ['classify', 'prod_spec_id'=>10002,   'parent_id'=> $model->id ], ['title' => "收录房源"]);
        return $sumCanvass . $unit ;
    };

    //$i++;
    $i = 'prod_lended_price';
    $columns[$i]['label'] = '在租均价';
    $columns[$i]['format'] = 'raw';

    $columns[$i]['options'] = ['style'=>'width:90px'];
    $columns[$i]['value']   = function($model)
    {

        $unit = '元';
        $model->lendedPrice = 0;
        $tmp = $model->getLendedPrices($model->id, $model->prod_spec_id);
        if ($tmp['sumLendedPrices'])
        {
            $model->lendedPrice = $tmp['sumLendedPrices'];
        }
        //Yii::trace("=====================              ============");
        //  $sumLendedContent = $model->lendedProds->lendedAreas->sumLendedContent;
        //Yii::trace($tmp );
        //return Html::a(count($model->subProdInsts), ['classify', 'prod_spec_id'=>10002,   'parent_id'=> $model->id ], ['title' => "收录房源"]);
        return  $model->lendedPrice . $unit;
    };



    //$i++;
    $i = 'prod_use_cust_count';
    $columns[$i]['label'] = '入驻企业';
    $columns[$i]['format'] = 'raw';
    $columns[$i]['options'] = ['style'=>'width:90px'];
    $columns[$i]['value']   = function($model)
    {
        $unit = '家';
        $tmp = $model->getUseCustCount($model->id, $model->prod_spec_id);
//            Yii::trace("=====================              ============");
//            Yii::trace($tmp);

        if (!$tmp)
        {
            $tmp=0;
        }

        return $tmp . $unit;
    };


        if($prod_spec_id==10002){
            //$i++;
            //名称
            $i = 'prod_state';
            $columns[$i]['label'] = '房源状态';
            $columns[$i]['attribute'] = 'state';
            $columns[$i]['options'] = ['style'=>'width:180px'];
            $columns[$i]['value']   = function($model) use ($domainTypes)
            {
                return $domainTypes[$model->state];

            };
        }


        // 如果是楼盘需要取出子集
        if ($prod_spec_id==10001 || $prod_spec_id==10003 ||  $prod_spec_id==10004)
        {

            //$i++; // 区域
            $i = 'prod_cnt';
            $columns[$i]['label'] = '收录房源';
            $columns[$i]['format'] = 'raw';

            $columns[$i]['options'] = ['style'=>'width:90px'];
            $columns[$i]['value']   = function($model) use ($sub_prod_spec_id ,$url ,$yw2)
            {
                if($yw2==3){
                    return count($model->subProdInsts);
                }else{
                    return Html::a(count($model->subProdInsts), ['classify', 'prod_spec_id'=> $sub_prod_spec_id ,   'parent_id'=> $model->id ,'a'=>$url ], ['title' => "收录房源"]);

                }

            };

          if ($prod_spec_id==10001)
          {

              $subStaffGroups =  Functions::getDomains('staff','sub_staff_group');

              $subStaffGroups[''] = '全部';
              ksort($subStaffGroups);
              //$i++; // 区域
              $i = 'prod_trading_area';
              $columns[$i]['label'] = '商圈';
              $columns[$i]['format'] = 'raw';
              //  $columns[$i]['attribute'] = 'parentProdInsts.prod_name';
              $columns[$i]['options'] = ['style'=>'width:120px'];
              $columns[$i]['filter'] = Html::activeDropDownList($searchModel, 'sub_staff_group',$subStaffGroups,['style'=>'height:24px;width: 60px;']);
              $columns[$i]['value']   = function($model) use($prodSpecName,$prod_spec_id,$now_url)
              {
                  $mdBtn = [];
                  if (!$model->parentProdInsts)
                  {
                      $mdBtn['id'] 					    =  'activity-update-prod-link';
                      $mdBtn['class'] 				    =  "activity-update-prod-link";
                      $mdBtn['data-toggle']			= 'modal';
                      $mdBtn['data-target']			= '#update-prod-modal';
                      $mdBtn['label']					= '设置商圈';
                      $mdBtn['title']					=  '设置商圈';
                      $mdBtn['data-prod_id'] 			= $model->id;
                      $mdBtn['data-prod_spec_id'] 	=  $prod_spec_id;
                      $mdBtn['data-last_url' ] 		=  $now_url;
                      $mdBtn['data-prod_spec_name'] 	=  $prodSpecName;
                      return Html::a('设置商圈', '#', $mdBtn);

                      //return Html::a('设置商圈', ['update', 'prod_spec_id'=>10001,   'id'=> $model->id ,    'prod_spec_name' => $prodSpecName], ['title' => "商圈"]);

                  }



                  $mdBtn['id' ]					        = 	'activity-view-link';
                  $mdBtn['class' ]				        = 	"activity-view-link";
                  $mdBtn['data-toggle']	            =	'modal';
                  $mdBtn['data-target']			    =	'#view-modal';
                  $mdBtn['label']					    =	'查看商圈';
                  $mdBtn['title' ]				        = 	'查看商圈';
                  $mdBtn['data-prod-id']			    =	$model->id;
                  $mdBtn['data-prod-spec-id']        = 	10004;
                  $mdBtn['data-prod-spec-name' ]    	= 	'查看商圈';
                  return Html::a($model->parentProdInsts->prod_name, '#', $mdBtn);

                 // return Html::a($model->parentProdInsts->prod_name, ['view', 'id'=>$model->parentProdInsts->id,  'prod_spec_id'=>10004,  'prod_spec_name' =>'商圈'  ], ['title' => "商圈"]);

              };
          }

        }
        else if ($prod_spec_id==10002)
        {
            //$i++; // 区域
            $i = 'prod_trading_buliding';
            $columns[$i]['label'] = '归属楼盘';
            $columns[$i]['format'] = 'raw';
            //  $columns[$i]['attribute'] = 'parentProdInsts.prod_name';
            $columns[$i]['options'] = ['style'=>'width:180px'];
            $columns[$i]['value']   = function($model) use($prodSpecName)
            {
                if (!$model->parentProdInsts)
                {
                    return Html::a('设置楼盘', ['update', 'prod_spec_id'=>10002,   'id'=> $model->id ,    'prod_spec_name' => $prodSpecName], ['title' => "归属楼盘"]);

                }
                return Html::a($model->parentProdInsts->prod_name, ['view', 'id'=>$model->parentProdInsts->id,  'prod_spec_id'=>10001,  'prod_spec_name' =>'楼盘'  ], ['title' => "归属楼盘"]);

            };
        }


        //$i++;




        //

        foreach ($attrSpecs as $attrSpec)
        {
            $prodAttr = $attrSpec->attrSpecs;

            //$i++;

            if($attrSpec->grid_show_flag==1)
            {
                $i = $prodAttr->attr_field_name;;
                $columns[$i]['label'] = $prodAttr->attr_spec_name;
                $prodAttrCodes[$i] = $prodAttr->attr_spec_id;
                $a = $prodAttr->attr_spec_id;
                //$columns[$i]['attribute']  = $prodAttr->attr_spec_id;
                $columns[$i]['value']   = function($model, $key, $index, $column)use ($a)
                {


                    foreach ($model->prodAttrInsts  as $prodAttrInst)
                    {


                        if ($a == $prodAttrInst->attr_spec_id)
                        {

                            return $prodAttrInst->content;
                        }
                    }

                };


                $columns[$i]['options'] = ['style'=>'width:180px'];
            }

        }






//        //$i++; // 区域
//        $columns[$i]['label'] = '二级区域名称'. $regionTypes[3];
//        //$columns[$i]['attribute'] = 'parentRegions.region_name';
//        $columns[$i]['options'] = ['style'=>'width:180px'];
//        $columns[$i]['content']   = function($model)
//        {
//            global $regionTypes2;
//            return $model->parentRegions->region_name  ;
//        };



//        //$i++; // 区域
//        $columns[$i]['label'] = '区域名称';
//        $columns[$i]['options'] = ['style'=>'width:180px'];
//        $columns[$i]['content']   = function($model)
//        {
//
//            return $model->parentRegions->region_name . ' ' . $model->regions->region_name; //. $regionTypes2 [ $model->regions->region_type_id ]  ;
//        };
//        $columns[$i]['filter'] = Html::activeTextInput($searchModel, 'region_name', ['class'=>'form-control']);

/*
        foreach ($attrSpecs as $attrSpec)
        {
            $prodAttr = $attrSpec->attrSpecs;

                //$i++;



                $columns[$i]['label'] = $prodAttr->attr_spec_name;
                $prodAttrCodes[$i] = $prodAttr->attr_spec_id;
                $a = $prodAttr->attr_spec_id;
                //$columns[$i]['attribute']  = $prodAttr->attr_spec_id;
                $columns[$i]['value']   = function($model, $key, $index, $column)use ($a)
                {


                    foreach ($model->prodAttrInsts  as $prodAttrInst)
                    {


                        if ($a == $prodAttrInst->attr_spec_id)
                        {
                            return $prodAttrInst->content;
                        }
                    }
                    return '未设置具体的值！';
                };


                $columns[$i]['options'] = ['style'=>'width:180px'];

        }


        //$i++;
        //名称
        $columns[$i]['label'] = '产权客户';
        $columns[$i]['attribute'] = 'own_cust_id';
        $columns[$i]['options'] = ['style'=>'width:120px'];
        $columns[$i]['filter'] = Html::activeTextInput($searchModel, 'ownCustName', ['class'=>'form-control']);
        $columns[$i]['content']   = function($model)
        {
            if ($model->custs->cust_name)
            {
                return $model->custs->cust_name  ;
            }
            return '未指定'  ;
        };



        //$i++;
        //名称
        $columns[$i]['label'] = '创建人';
        $columns[$i]['attribute'] = 'create_staff_id';
        $columns[$i]['options'] = ['style'=>'width:180px'];
        $columns[$i]['filter'] = Html::activeTextInput($searchModel, 'user_name', ['class'=>'form-control']);
        $columns[$i]['content']   = function($model)
        {
            return $model->users->username  ;
        };


        //$i++;
        //名称
        $columns[$i]['label'] = '更新时间';
        $columns[$i]['attribute'] = 'modify_date';
        $columns[$i]['options'] = ['style'=>'width:180px'];

*/

        // 使用膜版进行二次加工
        $columns2 = Functions::getFormatColumns($customs, $columns,$searchModel);
        $ii = count($columns2);

        //$i++;
        //操作选项
        //$columns[$i]['class']   = 'app\common\grid\CustActionColumn';
        $columns2[$ii]['options'] = ['style'=>'width:110px'];

        $columns2[$ii]['content']   = function($model)use ($prodSpecName, $now_url)
        {
            $btns = [];

            $btnBegin = Html::a( '查看',  '#' ,
                ['id' => 'activity-view-link',
                    'class' => "btn btn-xs btn-warning activity-view-link",
                    'data-toggle'=>'modal',
                    'data-target'=>'#view-modal',
                    'label'=>'查看子产品', 'title' => '查看子产品',
                    'data-prod-id'=>$model->id,
                    'data-prod-spec-id'=> $model->prod_spec_id,
                    'data-prod-spec-name' => $prodSpecName]);

            $btns[]   = Html::a( '查看',  '#' ,
                ['id' => 'activity-view-link',
                    'class' => "activity-view-link",
                    'data-toggle'=>'modal',
                    'data-target'=>'#view-modal',
                    'label'=>'查看子产品',
                    'title' => '查看子产品',
                    'data-prod-id'=>$model->id,
                    'data-prod-spec-id'=> $model->prod_spec_id,
                    'data-prod-spec-name' => $prodSpecName]);

            $btns[]   = Html::a( '编辑',  '#' ,
                ['id' => 'activity-update-prod-link',
                    'class' => "activity-update-prod-link",
                    'data-toggle'=>'modal',
                    'data-target'=>'#update-prod-modal',
                    'label'=>'编辑产品',
                    'title' => '编辑产品',
                    'data-prod_id'=>$model->id,
                    'data-prod_spec_id'=> $model->prod_spec_id,
                    'data-last_url'  => $now_url,
                    'data-prod_spec_name' => $prodSpecName]);


//            $btns[]   = Html::a( '编辑', ['update', 'id'=>$model->id,   'prod_spec_id'=> $model->prod_spec_id,   'last_url'  => $now_url, 'prod_spec_name' => $prodSpecName ], [ 'label'=>'编辑', 'title' => '编辑']);
            $btns[]   = Html::a( '删除', ['delete', 'id'=>$model->id,   'prod_spec_id'=> $model->prod_spec_id , 'last_url'  => $now_url], [ 'label'=>'删除', 'title' => '删除']);


            return Functions::getActionDropdownContext($btnBegin, $btns);
            // return  substr($model->create_date,0,16) ;
            // date('Y-m-d H:i:s', $model->modify_date);
        };

//        $columns[$i]['buttons'] =  [
//            'view' => function ($url, $model, $key) use ($prodSpecName) {
//                return Html::a(Html::tag('span', '查看', ['class' => "glyphicon fa fa-eye"]), ['view', 'id'=>$model->id,   'prod_spec_id'=> $model->prod_spec_id,    'prod_spec_name' => $prodSpecName], ['class' => "btn btn-xs btn-success",'label'=>'查看子产品', 'title' => '查看子产品']);
//            },
//
//            'update' => function ($url, $model, $key) use ($prodSpecName) {
//                return Html::a(Html::tag('span', '编辑', ['class' => "glyphicon fa fa-edit"]), ['update', 'id'=>$model->id,   'prod_spec_id'=> $model->prod_spec_id,    'prod_spec_name' => $prodSpecName ], ['class' => "btn btn-xs btn-success",'label'=>'查看子产品', 'title' => '查看子产品']);
//            },
//
//            'delete' => function ($url, $model, $key)  {
//                return Html::a(Html::tag('span', '删除', ['class' => "glyphicon fa fa-trash"]), ['delete', 'id'=>$model->id,   'prod_spec_id'=> $model->prod_spec_id ], ['class' => "btn btn-xs btn-danger",'label'=>'删除产品', 'title' => '删除子产品']);
//            },
//
//
//            'create' => function ($url, $model, $key) use ($prodSpecName) {
//                return Html::a(Html::tag('span', '订单生成', ['class' => "glyphicon fa fa-edit"]), ['/prod/order/create', 'prod_id'=>$model->id,       'prod_spec_name' => $prodSpecName ], ['class' => "btn btn-xs btn-success",'label'=>'生成一张订单', 'title' => '查看子产品']);
//            },
//
//        ];


        echo GridView::widget([
        'dataProvider' => $dataProvider,
            'filterModel' => $searchModel,
        'layout' => "{items}{summary}{pager}",
        'options'=>['id'=>'grid'],
        'columns' => $columns2

        ]);


    ?>



